package com.wanger.swa.config.autoDDL.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.wanger.swc.config.myBatisPlus.autoDDL.AutoDDLService;
import com.wanger.swc.config.myBatisPlus.autoDDL.model.FieldMapper;
import com.wanger.swc.config.myBatisPlus.autoDDL.model.MyDBType;
import com.wanger.swc.exception.BusEx;
import com.wanger.swc.util.json.MyJson;
import com.wanger.swc.util.validation.MyVerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.anyline.metadata.type.DatabaseType;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author: wanger
 * @Date: 2025/3/12 21:42
 * @Description:
 */
@Slf4j
@Component
public class OceanBase_AutoDDLServiceImpl implements AutoDDLService {

    /**
     * 数据库实现类类型
     *
     * @return MyDBType
     */
    @Override
    public MyDBType getContentType() {
        return new MyDBType(com.oceanbase.jdbc.Driver.class, DatabaseType.OceanBase);
    }

    /**
     * 获取java类型对应的该数据库的字段类型
     *
     * @param javaType java类型
     * @return 数据库字段类型
     */
    @Override
    public String getDataType(Field javaType) {
        MyVerifyUtil.nullVerify(javaType, "javaType不能为空！");
        String name = javaType.getType().getSimpleName();
        switch (name) {
            case "byte":
            case "Byte":
                return "TINYINT";
            case "short":
            case "Short":
                return "SMALLINT";
            case "int":
            case "Integer":
                return "INTEGER";
            case "long":
            case "Long":
                return "BIGINT";
            case "float":
            case "Float":
                return "FLOAT";
            case "double":
            case "Double":
                return "DOUBLE";
            case "char":
            case "Character":
            case "String":
                return "VARCHAR";
            case "boolean":
            case "Boolean":
                return "BOOLEAN";
            case "Date":
                return "TIMESTAMP";
            default:
                throw BusEx.create(StrUtil.format("类型【{javaType}】未找到该数据库对应的类型配置，请在【{methodName}】方法内配置",
                        MyJson.map()
                                .pu("javaType", javaType)
                                .pu("methodName", getClass().getName() + "." + "getDataType(String javaType)")));
        }
    }

    /**
     * 获取该类型的字段类型映射
     *
     * @return 字段类型映射
     */
    @Override
    public List<FieldMapper> getFieldMappers() {
        return CollUtil.toList(
                //数值类型
                new FieldMapper("\\b(?:TINYINT)\\b", "Byte"),
                new FieldMapper("\\b(?:SMALLINT)\\b", "Short"),
                new FieldMapper("\\b(?:INT|INTEGER|YEAR)\\b", "Integer"),
                new FieldMapper("\\b(?:BIGINT)\\b", "Long"),
                new FieldMapper("\\b(?:FLOAT)\\b", "Float"),
                new FieldMapper("\\b(?:DOUBLE)\\b", "Double"),
                new FieldMapper("\\b(?:DECIMAL|NUMERIC)\\b", "BigDecimal", "import java.math.BigDecimal;"),
                //字符串类型
                new FieldMapper("\\b(?:CHAR|VARCHAR|TEXT|LONGTEXT|ENUM|SET|JSON)\\b", "String"),
                //日期类型
                new FieldMapper("\\b(?:DATE|TIME|DATETIME|TIMESTAMP)\\b", "Date", "import java.util.Date;", "import com.fasterxml.jackson.annotation.JsonFormat;"),
                //二进制类型
                new FieldMapper("\\b(?:BINARY|VARBINARY|BLOB|LONGBLOB|BIT)\\b", "byte[]"),
                //布尔类型
                new FieldMapper("\\b(?:BOOL|BOOLEAN)\\b", "Boolean"),
                //其他类型
                new FieldMapper("\\b(?:BIT)\\b", "Object")
        );
    }

}
